package com.liuchuanzheng.lcz_animationdemos.property.blur;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.SeekBar;

import androidx.appcompat.app.AppCompatActivity;

import com.liuchuanzheng.lcz_animationdemos.R;
import com.liuchuanzheng.lcz_animationdemos.databinding.ActivityBlurBinding;

public class BlurActivity extends AppCompatActivity {
    private ActivityBlurBinding mViewBinding;


    /**
     * 原始图片
     */
    private Bitmap mTempBitmap;

    /**
     * 模糊后的图片
     */
    private Bitmap mFinalBitmap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mViewBinding = ActivityBlurBinding.inflate(getLayoutInflater());
        setContentView(mViewBinding.getRoot());

        //获取图片
        mTempBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.scene);
        mFinalBitmap = BlurImage.blur(this, mTempBitmap, 25);


        // 填充模糊后的图像和原图
        mViewBinding.activityMainBluredImg.setImageBitmap(mFinalBitmap);
        mViewBinding.activityMainOriginImg.setImageBitmap(mTempBitmap);
        mViewBinding.activityMainOriginImg.setAlpha(0.0f);

        // 处理seekbar滑动事件
        setSeekBar();
    }

    /**
     * 处理seekbar滑动事件
     */
    private void setSeekBar() {
        mViewBinding.activityMainSeekbar.setMax(100);
        mViewBinding.activityMainSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                float value = (float) progress / 100.0f;
                Log.e("seek", progress + "the valus is " + value);
                mViewBinding.activityMainOriginImg.setAlpha(value);
                mViewBinding.activityMainProgressTv.setText(String.valueOf(progress));

                float blurValue = 25 - progress / 4;
                mFinalBitmap = BlurImage.blur(BlurActivity.this, mTempBitmap, blurValue);
                mViewBinding.activityMainBluredImg.setImageBitmap(mFinalBitmap);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.zoomout);
    }
}